package dao;

import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class newDB {
	
	public Connection con;
	
    public int dbOpen(){
    	int hasil = 0;
        String dbname="jdbc:mysql://localhost:3306/simpleapp";
       // Connection con =null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection(dbname,"root","");
            con.setAutoCommit(false);
            if(con!=null)
                hasil = 1;
        }
        catch(ClassNotFoundException ex){
            System.err.println("Driver Error");
            ex.printStackTrace();
            System.exit(1);
        }
        catch(SQLException ex){
            System.err.println("Tidak berhasil Koneksi");
            System.exit(1);
        }
        return hasil;
    }
	
	public int deleteContent(String nama) {
		dbOpen();
		con = getConnection();
		Statement st;
		ResultSet rs;
		int ada=0;
		try{
            st = con.createStatement();
            String sql = "DELETE FROM pesan WHERE message='"+nama+"'";
            st.executeUpdate(sql);
            String sql2 = "SELECT message FROM pesan WHERE message='"+nama+"'";
            rs = st.executeQuery(sql2);
            while(rs.next())
            	ada++;
        }catch(Exception ex){
            System.err.println(ex.getMessage());
            System.exit(1);
        }
        return ada;
	}
	
    public Connection getConnection() {
        return con;
    }
}
